package com.kongwg.exer1;

import java.util.concurrent.*;

/**
 *
 * 用Callable创建綫程线程
 * @author kongwg
 * @create 2020-12-06 10:26
 */
class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println("I am Runnable");
    }
}

class MyCallable implements Callable {

    @Override
    public Object call() throws Exception {
        System.out.println("我是你的爸爸");
    return "早睡早起身体好";
    }

}
public class CallableTest {
    public static void main(String[] args) {
        MyCallable myCallable = new MyCallable();
        FutureTask futureTask = new FutureTask(myCallable);
        Thread thread = new Thread(futureTask);
        thread.start();
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        //线程池创建
        ExecutorService service = Executors.newFixedThreadPool(10);
        service.execute(new MyRunnable());
        Future submit = service.submit(new MyCallable());

        try {
            Object o = submit.get();
            System.out.println(o);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        service.shutdown();
    }
}
